package cn.jimmie.learn.viewArt.refreshview.core;

import android.animation.ValueAnimator;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.animation.AccelerateDecelerateInterpolator;

/**
 * function :
 *
 * <p> Create Time:  2019年04月11日 19:12 </p>
 * <p> @author Jimmie.Qian  </p>
 */
public abstract class DecorHolder implements ValueAnimator.AnimatorUpdateListener, OnViewAnimatorEvent {
    private static final float ANIMATION_SPEED_RATIO = 1.6f;
    private static final int DEFAULT_ANIMATION_DURATION = 300;
    private static final int LOAD_MORE_DURATION = 500;

    private Context mContext;
    private View mHeadView;
    private View mFootView;

    private MarginLayoutParams mHeaderParams;
    private ValueAnimator mAnimator = ValueAnimator.ofFloat(100);

    // 头部控件需要修正的长度
    // mHeadOffset>0? 是否需要刷新, 拉下位置 如果超过Header的高度则需要刷新,否则直接复原
    private int mHeadOffset;

    abstract View createHeaderView();

    abstract View createFooterView();

    DecorHolder(Context context) {
        this.mContext = context;
        mHeadView = createHeaderView();
        mFootView = createFooterView();
        mHeadView.post(new Runnable() {
            @Override
            public void run() {
                // 当View 被add 进ViewGroup中时, layoutParams才被创建
                mHeaderParams = (MarginLayoutParams) mHeadView.getLayoutParams();
                requestHeaderLayout(-mHeadView.getHeight());
            }
        });
        mAnimator.addUpdateListener(this);
        mAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
    }

    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        float rate = (float) animation.getAnimatedValue() / 100f;
        int offset = mHeadOffset > 0 ? 0 : -getHeadView().getHeight();
        requestHeaderLayout((int) (mHeadOffset + (offset - mHeadOffset) * rate));
        if (mHeadOffset > 0 && rate == 1) {
            onHeaderRefreshing();
        }
    }

    Context getContext() {
        return mContext;
    }

    public View getHeadView() {
        return mHeadView;
    }

    public View getFootView() {
        return mFootView;
    }

    public void requestHeaderLayout(int marginTop) {
        mHeaderParams.topMargin = marginTop;
        mHeadView.requestLayout();
    }

    public int getHeaderTopMargin() {
        return mHeaderParams.topMargin;
    }

    public void startHeaderAnimation(boolean isPulling) {
        mHeadOffset = getHeaderTopMargin();
        int duration = LOAD_MORE_DURATION;
        if (isPulling) {
            int position = mHeadOffset + mHeadView.getHeight();
            duration = position / ANIMATION_SPEED_RATIO < DEFAULT_ANIMATION_DURATION ?
                    DEFAULT_ANIMATION_DURATION : (int) (position / ANIMATION_SPEED_RATIO);
        }
        mAnimator.cancel();
        mAnimator.start();
        mAnimator.setDuration(duration);
    }

    public void release() {
        mAnimator.cancel();
        mAnimator.removeAllListeners();
        mAnimator.removeAllUpdateListeners();
    }
}
